<?php
defined('BASEPATH') OR exit('No direct script access allowed');

//用户数据
class UserData extends MY_Controller {

	function __construct(){
		parent::__construct();
		
	}	
	
	//饮食打卡
	function user_diets(){
		$user_id = $this->user_id;
		
		$page	= $this->input->post('page');
		$size	= $this->input->post('size');
		$type	= $this->input->post('type',null,0);//饮食 0全部 1早餐 2中餐 3晚餐 4加餐
		
		if($page<1)$page=1;
		if($size<1)$size=10;
		
		$arr = array(
			"user_id" => $user_id,
			"type"		=> $type
		);
		
		$this->load->model('User/UserDietsModel','diets');
		
		$res = $this->diets->get_list($arr,$page,$size);
		if(($page-1)*$size>$res['count']){
			$this->err_out('没有了',10);
		}
		
		$data['result_code'] = 0;
		$data['message']     = '获取列表成功';
		$data['data'] 		 = array('count'=>$res['count'],'list'=>$res['list']);
	
		$this->out($data);
	}

	//饮食打卡
	function user_diets_del(){
		$id	= $this->input->post('id',null,0);

		$this->load->model('User/UserDietsModel','diets');
		$this->diets->del("id='{$id}'");
		
		$data['result_code'] = 0;
		$data['message']     = '删除成功';
		$this->out($data);
	}
	
	//获取排课时间
	function user_appoint_time(){		
		/*
		$id	= $this->input->post('id');	//排课id
		$this->load->model('Index/CoursePaikeModel','paike');
		$info = $this->paike->get_info($id);
		if(empty($info)){
			$this->err_out('请选择课程');
		}*/
		
		
		/****/
		$id	= $this->input->post('id');	//我的课程id
		
		if(!$id){
			$this->err_out('请选择课程');
		}
		
		$this->load->model('User/UserCardModel','card');
		$card_info = $this->card->get_info($id);
		
		$info = array(
			"sdate" => date("Y-m-d",time()),//开始日期
			"stime" => '08:00',				//开始时间
			"etime" => '22:00',				//结束时间
			"duration"=>$card_info['duration'],//时长
			"interval"=>10				//课程时间间隔10分钟
		);
		/****/
		
		
		$stime = strtotime($info['sdate'].' '.$info['stime'].':00');		
		$etime = strtotime($info['sdate'].' '.$info['etime'].':00');
		
		//时长
		$duration = $info['duration']*60;
		//课程时间间隔
		$interval = $info['interval']*60;
		
		$timec = date("H:i",time());		
		$num   = intval(($etime-$stime)/($duration+$interval));
		
		$list = array();	
		for($i=0;$i<$num;$i++){			
			$ls_stime = date("H:i",$stime+$i*$duration+$i*$interval);
			$ls_etime = date("H:i",strtotime($ls_stime)+$duration);			
			
			//if($ls_stime - $timec > 0 ){
				$ls['stime'] = $ls_stime;
				$ls['etime'] = $ls_etime;			
				$list[] = $ls;
			//}
		}
		
		$data['result_code'] = 0;
		$data['message']     = '获取成功';
		$data['data'] 		 = $list;
		$this->out($data);
	}
	
	//我的课程
	function user_appoint_course(){
		$user_id = $this->user_id;
		$page	= $this->input->post('page');
		$size	= $this->input->post('size');
		
		if($page<1)$page=1;
		if($size<1)$size=10;
		
		$arr = array(
			"user_id" => $user_id
		);
		
		$this->load->model('User/UserCardModel','card');
		$res = $this->card->get_list($arr,$page,$size);	
		
		if(($page-1)*$size>$res['count']){
			$this->err_out('没有了',10);
		}
		
		$data['result_code'] = 0;
		$data['message']     = '获取列表成功';
		$data['data'] 		 = array('count'=>$res['count'],'list'=>$res['list']);
	
		$this->out($data);
	}
	
	//获取排课（不用了）
	public function get_appoint_class(){
		$this->err_out('接口已注销');
		
		$user_id= $this->user_id;		
		$date   = $this->input->post('date',null,'');	 //选择日期 2018-06-13
		$card_id   = $this->input->post('cid',null,'');	 //选课程 card  id		
		$arr = array(
			"user_id" => $user_id,
			"date"	  => $date,
			"card_id" => $card_id
		);		
		$this->load->model('Index/CoursePaikeModel','paike');
		$res = $this->paike->get_lists($arr);	
		
		$data['result_code'] = 0;
		$data['message']     = $res?'获取成功':'暂无数据';
		$data['data'] 		 = $res?$res:array();	
		$this->out($data);
	}
	
	//start
	/**
	* @name 添加预约
	* @param id true int 课程id
	* @param sdate true char 日期
	* @param stime true char 时间
	* @param coach_id false int 教练ID
	* @return 
	*/
	//end
	public function add_appoint(){
		$user_id = $this->user_id;
		$id	= $this->input->post('id');				
		
		$sdate	= $this->input->post('sdate');		
		$stime	= $this->input->post('stime');		
		$coach_id	= $this->input->post('coach_id',null,0);		
		
		$time = time();
		$this->load->model('User/UserCardModel','card');
		$info = $this->card->get_info($id);		
		if(empty($info)){
			$this->err_out('请选择课程');
		}		
		//验证归宿卡
		if($info['user_id']!=$user_id){
			$this->err_out('不能使用别人的卡');
		}		
		//验证会员卡次数
		if($info['num']==0){
			$this->err_out('卡余额不足');
		}		
		if(empty($stime)){
			$this->err_out('请选择课程日期');
		}		
		if(empty($stime)){
			$this->err_out('请选择课程时间');
		}		
			
		//限制最大人数
		// $this->load->model('User/UserAppointModel','appoint');
		// $sum = $this->appoint->get_join_num($sdate,$stime,$info['cid']);
		// if($info['max_num'] - $sum<=0){
		// 	$this->err_out('预约人数报名已满，请选择其他课时');
		// }
		
		//检查重复预约
		$repeat = $this->appoint->get_join_repeat($sdate,$stime,$info['cid'],$user_id);
		if($repeat){
			$this->err_out('您已预约了该课程');
		}
		if(empty($coach_id)){
			$coach_id = $info['coach_id'];
		}
		$arr = array(
			"user_id" 	=> $user_id,
			//"pid" 	=> $id,
			"cid" 		=> $info['cid'],
			"mid" 		=> $coach_id,
			"shop_id" 	=> $info['shop_id'],
			"card_id"	=> $id,
			"sdate" 	=> $sdate,
			"stime" 	=> $stime,			
			"addtime"	=> $time,
			"status"	=> 0
		);		
		$res = $this->appoint->add($arr);
		
		//扣除会员卡次数
		if($res){
			$num = $info['num'] - 1;
			$res_card = $this->card->edit(array("num"=>$num)," id='{$id}' ");
			
		}
		
		$data['result_code'] = $res?0:1;
		$data['message']     = $res?'预约成功':'预约失败';
		$data['data'] 		 = $res?$res:array();
		$this->out($data);
	}
	
	//我的训练动作
	function user_train_sport(){
		$user_id = $this->user_id;		
		$cid = $this->input->post('cid',null,0);//课程id
		
		$this->load->model('Index/CourseModel','course');		
		$info = $this->course->get_info($cid);
		
		if(!$info){
			$this->err_out('数据不存在');
		}
		$sport = $info['sport'];
		
		
		$data['result_code'] = 0;
		$data['message']     = '获取成功';
		$data['data'] 		 = $sport?$sport:array();
		$this->out($data);
	}
	
	//我的预约
	function user_appoint(){
		$user_id = $this->user_id;		
		$page = $this->input->post('page');
		$size = $this->input->post('size');
		$type = $this->input->post('type');//0未签到 1已签到 2已取消预约 不传获取全部
		
		if($page<1)$page=1;
		if($size<1)$size=10;		
		
		//预约课程		
		$this->load->model('User/UserAppointModel','appoint');
		$arr = array(
			"user_id" => $user_id,
			"type"	  => $type
		);
		
		$this->load->model('User/CoachModel','CoachModel');
		$coach_list = $this->CoachModel->get_lists();
		
		
		$res = $this->appoint->get_list($arr,$page,$size);		
		$list = $res['list'];
		
		foreach($list as $key=>$val){
			$list[$key]['coach_name'] = '';
			foreach($coach_list as $item){
				if($item['id']==$val['mid']){
					$list[$key]['coach_name'] = $item['nickname'];
				}
			}
		}
		
		$res['list'] = $list;
		if(($page-1)*$size>$res['count']){
			$this->err_out('没有了',10);
		}
		
		$data['result_code'] = 0;
		$data['message']     = '获取列表成功';
		$data['data'] = array('count'=>$res['count'],'list'=>$res['list']);
	
		$this->out($data);
	}
	
	//我的收藏
	function user_collection(){
		$user_id= $this->user_id;
		$page	= $this->input->post('page');
		$size	= $this->input->post('size');
		$type	= $this->input->post('type',null,0);//0 文章 1课程
		
		if($page<1)$page=1;
		if($size<1)$size=10;
		
		$arr = array(
			"user_id" =>$user_id
		);
		
		
		if($type==0){
			//文章收藏
			$this->load->model('Index/ArticleActiveModel','collection');			
		}else{
			//课程收藏
			$this->load->model('Index/CourseCollectionModel','collection');
		}
		
		
		$res = $this->collection->get_list($arr,$page,$size);
		
		
		if(($page-1)*$size>$res['count']){
			$this->err_out('没有了',10);
		}
		
		$data['result_code'] = 0;
		$data['message']     = '获取列表成功';
		$data['data']  		 = array('count'=>$res['count'],'list'=>$res['list']);	
		$this->out($data);
	}
	
	//start
	/**
	* @name 我的训练	
	* @param page true int 页面，默认为1
	* @param size true int 数量，默认为10
	* @return 
	*/
	//end
	function user_train(){
		$user_id = $this->user_id;
		
		$page	= $this->input->post('page');
		$size	= $this->input->post('size');
		
		if($page<1)$page=1;
		if($size<1)$size=10;
		
		$arr = array(
			"user_id" => $user_id			
		);
		
		$this->load->model('User/CourseTrainModel','train');
		$res = $this->train->get_list($arr,$page,$size);
		$list = $res['list'];
		
		foreach($list as $key=>$val){
			$list[$key]['addtime'] = date('Y-m-d H:i:s',$val['addtime']);
		}
		
		$res['list'] = $list;
		if(($page-1)*$size>$res['count']){
			$this->err_out('没有了',10);
		}
		
		$data['result_code'] = 0;
		$data['message']     = '获取列表成功';
		$data['data'] 		 = array('count'=>$res['count'],'list'=>$res['list']);	
		$this->out($data);
	}
	
	//start
	/**
	* @name 我的训练详细
	* @param id true int id
	* @return 
	*/
	//end
	function user_train_info(){
		$user_id = $this->user_id;
		$id	= $this->input->post('id');	
		
		$this->load->model('User/CourseTrainModel','train');		
		$res = $this->train->get_info($id,$user_id);
		
		if(empty($res)){
			$this->err_out('数据错误');
		}
		
		//获取详情训练内容
		$this->load->model('User/CourseTrainInfoModel','train_info');
		$this->load->model('Index/CourseModel','course');
		$this->load->model('Index/SportModel','sport');
		
		//饮食 yinshi 动作 dongzuo 课程 kecheng				
		$train_data=array(
			"yinshi"=>array(),
			"dongzuo"=>array(),
			"kecheng"=>array()
		);
		$train_list=$this->train_info->get_lists(array('tid'=>$res['id']));
		foreach($train_list as $kk=>$vv){
			$stime=$vv['stime'];			
			if($vv['type']=='yinshi'){
				$ls_data=array();
				if($vv['info']){
					$slist=json_decode($vv['info'],true);
					foreach($slist as $kkk=>$vvv){
						$ls_data[$kkk]['name']=$vvv;
						$ls_data[$kkk]['stime']=$stime;
					}
				}
				$train_data['yinshi'][] = $ls_data;
			}
			else if($vv['type']=='dongzuo'){
				
				$sp_list = json_decode($vv['info'],true);
				$ls_info = array();
				if($sp_list){
					foreach($sp_list as $k=>$v){
						if(!isset($v['id'])){
							continue;
						}
						$spinfo = $this->sport->get_info($v['id']);
						if($spinfo){
							$spinfo['rest'] = isset($v['rest'])?$v['rest']:'';
							$spinfo['duration'] = isset($v['duration'])?$v['duration']:'';
							$spinfo['frequency'] = isset($v['frequency'])?$v['frequency']:'';
							$spinfo['group'] = isset($v['group'])?$v['group']:'';
							$spinfo['stime'] = $stime;
							$ls_info[] = $spinfo;
						}
					}
				}

				$train_data['dongzuo'][] = $ls_info;
			}
			/*
			else if($vv['type']=='dongzuo'){
				$ls_data=array();
				if($vv['info']){
					//动作id
					$data_id = json_decode($vv['info'],true);
					foreach($data_id as $ss){
						$info=$this->sport->get_info($ss);
						$info['stime']=$stime;
						$ls_data[]=$info;
					}				
				}
				$train_data['dongzuo'][] = $ls_data;
			}
			*/
			else if($vv['type']=='kecheng'){					
				$ls_data=array();
				if($vv['info']){
					//课程id
					$data_id = json_decode($vv['info'],true);
					foreach($data_id as $ss){
						$info=$this->course->unique_get_info('id',$ss);
						$info['stime']=$stime;
						$ls_data[]=$info;
					}				
				}
				$train_data['kecheng'][] = $ls_data;
			}
		}
		$res['train_data']=$train_data;
		
		$data['result_code'] = 0;
		$data['message']     = '获取成功';
		$data['data'] 		 = $res;
		$this->out($data);
	}
	
	//start
	/**
	* @name 训练计划删除
	* @param id true int id
	* @return 
	*/
	//end
	function user_train_del(){
		$user_id = $this->user_id;
		$id	= $this->input->post('id',null,0);
		$this->load->model('User/CourseTrainModel','CourseTrainModel');
		$info = $this->CourseTrainModel->get_info($id,$user_id);
		if(!$info){
			$this->err_out('数据不存在');
		}

		$this->CourseTrainModel->del("id='{$id}'");
		$this->load->model('User/CourseTrainInfoModel','CourseTrainInfoModel');
		$this->CourseTrainInfoModel->del("tid='{$id}'");
		
		$this->data_out(1);
	}
	
	//start
	/**
	* @name 训练计划添加收藏
	* @param id true int 训练计划id
	* @param status true int 状态，0.设置取消，1.设置收藏
	* @return 
	*/
	//end
	function set_train_love(){
		$user_id = $this->user_id;
		
		$id	= $this->input->post('id',null,0);
		$status	= $this->input->post('status',null,0);

		
		
		$this->load->model('User/CourseTrainModel','CourseTrainModel');
		$info = $this->CourseTrainModel->get_info($id,$user_id);
		if(!$info){
			$this->err_out('数据不存在');
		}
		
		$this->CourseTrainModel->edit(array('is_love'=>$status),"id='{$id}'");
		
		$this->data_out(1);
	}
	//我的动作
	function user_sport(){
		$user_id = $this->user_id;
		$page		= $this->input->post('page');
		$size		= $this->input->post('size');
		
		
		
		if($page<1)$page=1;
		if($size<1)$size=10;
		
		$arr = array(
			"user_id" => $user_id
		);
		
		$this->load->model('User/UserSportModel', 'usersport');
		$res = $this->usersport->get_list($arr,$page,$size);
		
		if(($page-1)*$size>$res['count']){
			$this->err_out('没有了',10);
		}
	
		$data['result_code'] = 0;
		$data['message']     = '获取列表成功';
		$data['data'] = array('count'=>$res['count'],'list'=>$res['list']);
	
		$this->out($data);
	}
	
	//我的动作评估
	function user_sport_info(){
		$user_id = $this->user_id;
		$id	= $this->input->post('id');	
		
		$this->load->model('User/UserSportModel', 'usersport');
		$res = $this->usersport->get_info($id,$user_id);
		if(!$res){
			$this->err_out('没有数据');
		}		
		
		$this->load->model('User/UserSportPgModel', 'usersportpg');		
		$comment_list = $this->usersportpg->get_lists($res['sid'],$user_id);
		$list = $comment_list?$comment_list:array();
		
		$data['result_code'] = 0;
		$data['message']     = '获取成功';
		$data['data'] 		 = $list;
		$this->out($data);
	}
	
}
